{% extends "base.html" %}
{% block worker_submenu %}
    <ul class="nav nav-second-level">
        <li>
            <a href="#">{{ endpoint }}</a>
        </li>
    </ul>
{% endblock %}
{% block page_nav %}
    <h2>Worker {{ worker_metrics['details']['host_name'] }}</h2>
    <ul class="nav nav-pills nav-justified">
        <li class="active"><a data-toggle="pill" role="tab" href="#worker-env">Environment</a></li>
        <li><a data-toggle="pill" role="tab" href="#worker-cfg">Configurations</a></li>
        <li><a data-toggle="pill" role="tab" href="#worker-res">Resources</a></li>
        <li><a data-toggle="pill" role="tab" href="#worker-tasks">Tasks</a></li>
        <li><a data-toggle="pill" role="tab" href="#worker-slots">Slots</a></li>
        <li><a data-toggle="pill" role="tab" href="#worker-perf">Performance</a></li>
        <li><a href="./{{ endpoint }}/timeline">Timeline</a></li>
    </ul>
{% endblock %}
{% block body %}
    {% set worker_details = worker_metrics['details'] %}
    <div class="row">
        <div class="tab-content">
            <div id="worker-env" class="tab-pane fade in active">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>Host</td>
                        <td><pre class="plain-pre">{{ worker_details['host_name'] }} ({{ endpoint }})</pre></td>
                    </tr>
                    {% if worker_details['k8s_pod_name'] is defined %}
                        <tr>
                            <td>Kubernetes Pod</td>
                            <td>{{ worker_details['k8s_pod_name'] }}</td>
                        </tr>
                    {% endif %}
                    {% if worker_details['yarn_container_id'] is defined %}
                        <tr>
                            <td>Yarn Container ID</td>
                            <td>{{ worker_details['yarn_container_id'] }}</td>
                        </tr>
                    {% endif %}
                    <tr>
                        <td>Platform</td>
                        <td><pre class="plain-pre">{{ worker_details['platform'] }}</pre></td>
                    </tr>
                    {% if worker_details['cuda_info'] is defined %}
                        <tr>
                            <td>CUDA</td>
                            <td><pre class="plain-pre">{{ worker_details['cuda_info'] }}</pre></td>
                        </tr>
                    {% endif %}
                    <tr>
                        <td>Git Branch</td>
                        <td><pre class="plain-pre">{{ worker_details['git_info'] }}</pre></td>
                    </tr>
                    <tr>
                        <td>Command</td>
                        <td><pre class="plain-pre" style="white-space: inherit">{{ worker_details['command_line'] }}</pre></td>
                    </tr>
                    <tr>
                        <td>Python</td>
                        <td><pre class="plain-pre">{{ worker_details['python_version'] }}</pre></td>
                    </tr>
                    <tr>
                        <td>Packages</td>
                        <td><pre class="plain-pre">{{ worker_details['package_info'] }}</pre></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div id="worker-cfg" class="tab-pane fade">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for key, value in worker_details['options'].items() %}
                        <tr>
                            <td>{{ key }}</td>
                            <td><pre class="plain-pre">{{ value }}</pre></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div id="worker-res" class="tab-pane fade">
                <div><h3>General</h3></div>
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>CPU Info</td>
                        <td><pre class="plain-pre">Usage: {{ '%0.2f' % worker_metrics['hardware']['cpu_used'] }}
Total: {{ worker_metrics['hardware']['cpu_total'] }}</pre></td>
                    </tr>
                    <tr>
                        <td>Memory Info</td>
                        <td><pre class="plain-pre">Usage: {{ worker_metrics['hardware']['memory_used'] | readable_size }}
Total: {{ worker_metrics['hardware']['memory_total'] | readable_size }}</pre></td>
                    </tr>
                    <tr>
                        <td>Memory Quota Info</td>
                        <td><pre class="plain-pre">Allocated: {{ worker_metrics['hardware']['mem_quota_used'] | readable_size }}
Hold: {{ worker_metrics['hardware']['mem_quota_hold'] | readable_size }}
Total: {{ worker_metrics['hardware']['mem_quota_total'] | readable_size }}</pre></td>
                    </tr>
                    <tr>
                        <td>Cache Info</td>
                        <td><pre class="plain-pre">Hold: {{ worker_metrics['hardware']['cached_hold'] | readable_size }}
Total: {{ worker_metrics['hardware']['cached_total'] | readable_size }}</pre></td>
                    </tr>
                    {% if worker_metrics['hardware']['disk_read'] is defined %}
                        <tr>
                            <td>Disk IO Info</td>
                            <td><pre class="plain-pre">Read Speed: {{ worker_metrics['hardware']['disk_read'] | readable_size }}/s
Write Speed: {{ worker_metrics['hardware']['disk_write'] | readable_size }}/s</pre></td>
                        </tr>
                    {% endif %}
                    {% if worker_metrics['hardware']['net_receive'] is defined %}
                        <tr>
                            <td>Network IO Info</td>
                            <td><pre class="plain-pre">Receive Speed: {{ worker_metrics['hardware']['net_receive'] | readable_size }}/s
Send Speed: {{ worker_metrics['hardware']['net_send'] | readable_size }}/s</pre></td>
                        </tr>
                    {% endif %}
                    {% if worker_metrics['hardware']['iowait'] is defined %}
                        <tr>
                            <td>IOWait</td>
                            <td><pre class="plain-pre">Percent: {{ worker_metrics['hardware']['iowait']}}%</pre></td>
                        </tr>
                    {% endif %}
                    <tr>
                        <td>Disk Info</td>
                        {% if worker_metrics['hardware']['disk_used'] is defined %}
                            <td><pre class="plain-pre">Bytes Used: {{ worker_metrics['hardware']['disk_used'] | readable_size }}
Bytes Total: {{ worker_metrics['hardware']['disk_total'] | readable_size }}
INode Used: {{ worker_metrics['hardware']['inode_used'] }}
INode Total: {{ worker_metrics['hardware']['inode_total'] }}</pre></td>
                        {% else %}
                            <td>Not available</td>
                        {% endif %}
                    </tr>
                    <tr>
                        <td>Update Time</td>
                        <td>{{ worker_metrics['update_time'] | format_ts }}</td>
                    </tr>
                    </tbody>
                </table>
                {% if worker_metrics['hardware']['cuda_stats'] is defined %}
                    <div><h3>GPUs</h3></div>
                    <table class="table">
                        <thead>
                        <tr>
                            <th>Item</th>
                            <th>Value</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for item in worker_metrics['hardware']['cuda_stats'] %}
                            <tr>
                                <td>{{ item['product_name'] }}</td>
                                <td><pre class="plain-pre">GPU Usage: {{ '%0.2f' % item['gpu_usage'] }}
Memory Usage: {{ item['fb_memory_used'] | readable_size }}
Memory Total: {{ item['fb_memory_total'] | readable_size }}
Temperature: {{ item['temperature'] }} C</pre></td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
                {% if worker_metrics['hardware']['disk_stats'] is defined %}
                    <div><h3>Disks</h3></div>
                    <table class="table">
                        <thead>
                        <tr>
                            <th>Item</th>
                            <th>Value</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for key, value in worker_metrics['hardware']['disk_stats'].items() %}
                            <tr>
                                <td>{{ key }}</td>
                                <td><pre class="plain-pre">Bytes Used: {{ value['disk_used'] | readable_size }}
Bytes Total: {{ value['disk_total'] | readable_size }}
INode Used: {{ value['inode_used'] }}
INode Total: {{ value['inode_total'] }}</pre></td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
            </div>
            <div id="worker-tasks" class="tab-pane fade">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        {% if progresses['ALLOCATING'] is defined %}
                            <td>Allocating ({{ progresses['ALLOCATING']['total'] }})</td>
                            <td>{{ progresses['ALLOCATING']['operands'] }}</td>
                        {% else %}
                            <td>Allocating</td>
                            <td>Not available</td>
                        {% endif %}
                    </tr>
                    <tr>
                        {% if progresses['PREPARING_INPUTS'] is defined %}
                            <td>Preparing Data ({{ progresses['PREPARING_INPUTS']['total'] }})</td>
                            <td>{{ progresses['PREPARING_INPUTS']['operands'] }}</td>
                        {% else %}
                            <td>Preparing Data</td>
                            <td>Not available</td>
                        {% endif %}
                    </tr>
                    <tr>
                        {% if progresses['CALCULATING'] is defined %}
                            <td>Running ({{ progresses['CALCULATING']['total'] }})</td>
                            <td>{{ progresses['CALCULATING']['operands'] }}</td>
                        {% else %}
                            <td>Running</td>
                            <td>Not available</td>
                        {% endif %}
                    </tr>
                    <tr>
                        {% if progresses['STORING'] is defined %}
                            <td>Storing ({{ progresses['STORING']['total'] }})</td>
                            <td>{{ progresses['STORING']['operands'] }}</td>
                        {% else %}
                            <td>Storing</td>
                            <td>Not available</td>
                        {% endif %}
                    </tr>
                    </tbody>
                </table>
            </div>
            <div id="worker-slots" class="tab-pane fade">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for key, value in worker_metrics['slots'].items() %}
                        <tr>
                            <td>{{ key }}</td>
                            <td>{{ value }}</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div id="worker-perf" class="tab-pane fade">
                {% set stats = worker_metrics['stats'] %}
                <table class="table">
                    <thead>
                    <tr>
                        <th>Item</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>Min Finish Time</td>
                        {% if stats['min_est_finish_time'] is defined %}
                            <td>{{ stats['min_est_finish_time'] | format_ts }}</td>
                        {% else %}
                            <td> N/A </td>
                        {% endif %}
                    </tr>
                    <tr>
                        <td>Max Finish Time</td>
                        {% if stats['max_est_finish_time'] is defined %}
                            <td>{{ stats['max_est_finish_time'] | format_ts }}</td>
                        {% else %}
                            <td> N/A </td>
                        {% endif %}
                    </tr>
                    {% if stats['net_transfer_speed'] is defined %}
                        <tr>
                            {% set value = worker_metrics['stats']['net_transfer_speed'] %}
                            <td>Networking</td>
                            <td>{{ value['mean'] | readable_size }}/s std: {{ value['std'] | readable_size }}/s count: {{ value['count'] | int }}</td>
                        </tr>
                    {% endif %}
                    {% if stats['disk_read_speed'] is defined %}
                        <tr>
                            {% set value = worker_metrics['stats']['disk_read_speed'] %}
                            <td>Disk Reads</td>
                            <td>{{ value['mean'] | readable_size }}/s std: {{ value['std'] | readable_size }}/s count: {{ value['count'] | int }}</td>
                        </tr>
                    {% endif %}
                    {% if worker_metrics['stats']['disk_write_speed'] is defined %}
                        <tr>
                            {% set value = worker_metrics['stats']['disk_write_speed'] %}
                            <td>Disk Writes</td>
                            <td>{{ value['mean'] | readable_size }}/s std: {{ value['std'] | readable_size }}/s count: {{ value['count'] | int }}</td>
                        </tr>
                    {% endif %}
                    {% if stats['wait_time.MemQuota'] is defined %}
                        <tr>
                            {% set value = worker_metrics['stats']['wait_time.MemQuota'] %}
                            <td>MemQuota Wait Time</td>
                            <td>{{ value['mean'] | round(2) }} std: {{ value['std'] | round(2) }} count: {{ value['count'] | round(2) }}</td>
                        </tr>
                    {% endif %}
                    {% for key, value in worker_metrics['stats'].items() %}
                        {% if key.startswith('calc_speed.') %}
                            <tr>
                                <td>Calc Rate of {{ key[11:] }}</td>
                                <td>{{ value['mean'] | readable_size }}/s std: {{ value['std'] | readable_size }}/s count: {{ value['count'] | int }}</td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock %}
